#include "regionlistctrl.h"

#include <vector>

#include <wx/log.h>
#include <wx/app.h>
#include <wx/msgdlg.h>

#include "RegionEditDlg.h"

const long RegionListCtrl::ID_REGION_EDIT_DLG = wxNewId();
const long RegionListCtrl::ID_REGION_CREATE_DLG = wxNewId();

RegionListCtrl::RegionListCtrl(wxWindow* parent, wxWindowID id,
    const wxPoint& pos, const wxSize& size, long style,
    const wxValidator& validator, const wxString& name)
    :wxListCtrl(parent, id, pos, size, style, validator, name), m_regionEditDlg(NULL)
{
    InsertColumn(0, wxT("地区码"));
    InsertColumn(1, wxT("国家(简体)"));
    InsertColumn(2, wxT("国家(繁体)"));
    InsertColumn(3, wxT("国家(英文)"));
    InsertColumn(4, wxT("省份(简体)"));
    InsertColumn(5, wxT("省份(繁体)"));
    InsertColumn(6, wxT("省份(英文)"));
    InsertColumn(7, wxT("城市(简体)"));
    InsertColumn(8, wxT("城市(繁体)"));
    InsertColumn(9, wxT("城市(英文)"));
    m_regionEditDlg = new RegionEditDlg(this, ID_REGION_EDIT_DLG);
    m_regionCreateDlg = new RegionEditDlg(this, ID_REGION_CREATE_DLG);
}

void RegionListCtrl::Load(const wxString& fileName, const wxString & filter)
{
    if(m_regionManager.Load(fileName))
    {
        Reload(filter);
        //wxLogMessage(wxT("Region size=%d"), m_regionManager.GetRegions().size());
    }
}

void RegionListCtrl::Reload(const wxString& filter)
{
    Load(m_regionManager.GetRegions(filter));
}


void RegionListCtrl::Load(const vector<Region>& regionList)
{
    Freeze();
    DeleteAllItems();
    m_itemData.clear();
    for(const Region & region: regionList) AddRegion(region);
    m_loaded = true;
    FitColumn();
    Thaw();
    wxApp::GetInstance()->Yield(true);
}

int RegionListCtrl::AddRegion(const Region& region)
{
    long index = InsertItem(GetItemCount(), region.GetLangCodeName());
    m_itemData.push_back(array<long, 10>());
    #define SET_ITEM_TEXT(column, type) \
        SetItemTextAndData(index, column, region.Get##type().GetName(SIMPLE_CHINESE), region.Get##type().GetId()); \
        SetItemTextAndData(index, column + 1, region.Get##type().GetName(TRADITIONAL_CHINESE), region.Get##type().GetId()); \
        SetItemTextAndData(index, column + 2, region.Get##type().GetName(ENGLISH), region.Get##type().GetId());

    SET_ITEM_TEXT(1, Country);
    SET_ITEM_TEXT(4, Province);
    SET_ITEM_TEXT(7, City);
    return 0;
    #undef SET_ITEM_TEXT
}

void RegionListCtrl::FitColumn()
{
    for(int i=0; i<GetColumnCount(); ++i)
    {
        SetColumnWidth(i, GetItemCount() ? wxLIST_AUTOSIZE : wxLIST_AUTOSIZE_USEHEADER );
    }
}

wxString RegionListCtrl::GetItemText(long index, long column) const
{
    wxListItem item;
    item.SetId(index);
    item.SetColumn(column);
    item.SetMask(wxLIST_MASK_TEXT);
    if( !this->GetItem(item) )
    {
        wxLogError(wxT("ERR %s: Get item(%ld, %ld) error!"), index, column);
        return wxT("");
    }
    return item.GetText();
}

bool RegionListCtrl::Edit(long index)
{
    m_regionEditDlg->InitEditDlg(index);
    if( m_regionEditDlg->ShowModal() == wxID_OK )
    {
        return true;
    }
    else
    {
        return false;
    }
}

bool RegionListCtrl::CreateRegion()
{
    m_regionCreateDlg->InitCreateDlg();
    if( m_regionCreateDlg->ShowModal() == wxID_OK )
    {
        return true;
    }
    else
    {
        return false;
    }
}


int RegionListCtrl::SetItemTextAndData(long index, long column, const wxString& text, long data)
{
    wxListItem item;
    item.SetId(index);
    item.SetColumn(column);
    item.SetText(text);
    item.SetMask(wxLIST_MASK_TEXT);
    if( !SetItem(item)  )
    {
        wxLogError(wxT("ERR SetItemTextAndData: SetItemText(%ld, %ld) %s Error!"),
            index, column, text.c_str());
        return -1;
    }
    m_itemData[index][column] = data;
    return 0;
}

int RegionListCtrl::UpdateItemText(long index, long column, const wxString& text)
{
    if( text.empty() )
    {
        wxLogError(wxT("ERR %s: text is empty!"), __func__);
        return -1;
    }
    int ret = -1;
    int fieldId = GetItemData(index, column);
    if( fieldId == 0 )
    {
        wxLogError(wxT("ERR %s: itemData is empty!"), __func__);
        return -1;
    }
    switch(column)
    {
    case 1:
    case 4:
    case 7:
        ret = m_regionManager.UpdateField(fieldId, SIMPLE_CHINESE, text);
        break;
    case 2:
    case 5:
    case 8:
        ret = m_regionManager.UpdateField(fieldId, TRADITIONAL_CHINESE, text);
        break;
    case 3:
    case 6:
    case 9:
        ret = m_regionManager.UpdateField(fieldId, ENGLISH, text);
        break;
    }
    if( ret !=0 )
    {
        return ret;
    }
    return 0;
}

long RegionListCtrl::GetItemData(long index, long column)
{
    return m_itemData[index][column];
}
